package com.nicolasprof.profgallery.utils {
	import com.nicolasprof.profgallery.Engine;
	import flash.events.*;
	import flash.utils.Timer;

	/**
	 * @author Nicolas Prof
	 */
	public class SlideShow extends Object {

		private var _mainTimer : Timer;
		private var _engine : Engine;
		protected var _isEnabled : Boolean;

		
		public function SlideShow($engine : Engine) {
			//INIT
			var config: XML = Engine.CONFIG_XML.slideShow[0];
			_engine = $engine;
			_mainTimer = new Timer(Number(config.@delay) * 1000);
			
			//LISTENERS
			_mainTimer.addEventListener(TimerEvent.TIMER, onTimer);
			_engine.imageHolder.addEventListener(Event.COMPLETE, onImageHolderReady, false, 0, true);
			
		}
		protected function start() : void {
			_mainTimer.start();
		}
		
		protected function stop() : void {
			_mainTimer.reset();
		}
		public function toggle() : void {
			this.enabled = !_isEnabled;
		}
		/**
		 * EVENTS
		 */		
		protected function onImageHolderReady(e : Event) : void {
			if(_isEnabled){
				this.start();
			}
		}
		protected function onTimer(e : TimerEvent) : void {				
			this.stop();
			_engine.selectThumbnail(1);
		}
		/**
		 * Getters / Setters
		 */
		public function set enabled($state : Boolean) : void {
			_isEnabled = $state;
			($state) ? this.start() : this.stop();
		}
	}
}
